package com.pata.display
{
	import com.pata.enum.InvalidationType;
	import com.pata.skin.GradientShape;
	import com.pata.utils.DataProvider;
	
	import flash.display.Shape;
	import flash.display.Sprite;

	/**
	 * @data	2011-4-6	
	 * @author	pata
	 */
	public class List extends AbstractDisplay implements IDisplay
	{
		private var mDataProvider:DataProvider;
		public function get dataProvider():Object
		{
			return mDataProvider;
		}
		public function set dataProvider(value:Object):void
		{
			mDataProvider = new DataProvider(value);
			this.invalidate( InvalidationType.DATA );
		}
		private var mSelectedIndex:int;
		public function get selectedIndex():int
		{
			return mSelectedIndex;
		}
		public function set selectedIndex(value:int):void
		{
			mSelectedIndex = value;
		}
		
		private var mRowCount:int;
		public function get rowCount():int
		{
			return mRowCount;
		}
		public function set rowCount(value:int):void
		{
			mRowCount = value;
			this.invalidate( InvalidationType.SIZE );
		}
		/**
		 * 每列高度
		 */
		private var mRowHeight:Number;
		public function get rowHeight():Number
		{
			return mRowHeight;
		}
		public function set rowHeight(value:Number):void
		{
			mRowHeight = value;
			this.invalidate( InvalidationType.SIZE );
		}
		
		private var mItemHolder:Sprite;
		private var mListItems:Array;
		private var mMaskShape:Shape;
		public function List()
		{
			super( this );			
		}
		override protected function configUI():void
		{
			createHolder();
			createMask();
			createList();
		}
		private function createHolder():void
		{
			mItemHolder = addChild(new Sprite) as Sprite;
		}		
		private function createList():void
		{
			while(mItemHolder.numChildren > 0)
			{
				mItemHolder.removeChildAt(0);				
			}
			mListItems = [];
			for(var i:int = 0;i < dataProvider.length; i++)
			{
				var item:BaseButton = new BaseButton();
				mListItems.push(item);
				mItemHolder.addChild( item );
			}
			
			if(mMaskShape == null && mListItems.length > mRowCount && mRowCount != 0)
			{
				createMask();
				createScrollbar();
			}
		}
		private function createMask():void
		{
			mMaskShape = new GradientShape(mWidth, mRowCount*mRowHeight, 0xff0000);
			addChild(mMaskShape);
			mItemHolder.mask = mMaskShape;
		}
		private function createScrollbar():void
		{
			
		}
		public function removeItemAt(index:int):void
		{
			mDataProvider.removeItemAt(index);
			createList();
		}
		override public function draw():void
		{
			if( isInvalid( InvalidationType.DATA ))
			{
				createList();
			}
			if( isInvalid( InvalidationType.SIZE ))
			{
				drawLayout();
			}
		}
		private function drawLayout():void
		{
			for(var i:int = 0; i < mListItems.length ; i++)
			{
				mListItems[i].height = mRowHeight;
			}
		}
	}
}